<?php

function setAmlaDate($manuyNumber)
{  
      require("../includes/openConnection.php"); 
 
    $sql_query="select MAX(AMLOT.recived) as maxAmlaDate,MIN(AMLOT.recived) as minAmlaDate,manuy2client.from_2
FROM
(
SELECT recived
FROM cash
WHERE  manuy2client_manuy_number = {$manuyNumber}
UNION
SELECT repayment_day
FROM checkes
WHERE  manuy2client_manuy_number = {$manuyNumber}
UNION
SELECT last_Payment_In	
FROM slikot
WHERE  manuy_number= {$manuyNumber}
) as AMLOT, manuy2client WHERE  manuy_number= {$manuyNumber}";
                      
                $result = mysql_query($sql_query) or die(mysql_error());

                if(mysql_num_rows($result) > 0)
		{
                     
                    $row = mysql_fetch_array($result);
                    $maxAmlaDate =  $row['maxAmlaDate'];
                    $minAmlaDate =  $row['minAmlaDate'];
                    $from_2 =  $row['from_2'];
                    
                    if(date("m",strtotime($from_2)) == date("m",strtotime($minAmlaDate)))
                        $recived =  $minAmlaDate;
                    else
                    $recived =  $maxAmlaDate;
                    
                     $sql_query2="UPDATE  manuy2client SET  `agentAmlaDate` =  '{$recived}' WHERE  manuy_number ={$manuyNumber};";
                   //echo $sql_query2;
                     mysql_query($sql_query2) or die(mysql_error());
                }
                
              
}


function changeDateFormatToDDMMYYY($date)
{  
$old_date = strtotime($date);         
$new_date = date('Y-m-d', $old_date); 
return $new_date;

}


?>